package com.xp.flower.qsd.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xp.flower.api.IFlowerLoveService;
import com.xp.flower.common.ajax.AjaxError;
import com.xp.flower.common.ajax.AjaxResponse;
import com.xp.flower.common.dto.FlowerLoveDTO;
import com.xp.flower.common.dto.UserDTO;
import com.xp.flower.qsd.consts.WebConsts;
@RequestMapping("flowerLoveController")
@Controller
public class FlowerLoveController {
    
    @Autowired
    private IFlowerLoveService flowerLoveServiceImpl;
    
    @RequestMapping(value = "addOrChannelLove", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse<FlowerLoveDTO> addOrChannelLove(
           @RequestParam(value = "flowerId", required = true)Long flowerId,
           @RequestParam(value = "loveOrChannel",required = true)Integer loveOrChannel,
           @RequestParam(value = "id",required = false)Long id,
           HttpSession session)
    {
        AjaxResponse<FlowerLoveDTO> reponse = null;
        UserDTO userDTO = ( UserDTO)session.getAttribute(WebConsts.USERINFO);
        
        if(null == userDTO){
            //提示用户跳转到登录页面
        }else{
            //喜欢
            if(loveOrChannel == 1){
                FlowerLoveDTO flowerLoveDTO = new FlowerLoveDTO();
                flowerLoveDTO.setUserId(userDTO.getId());
                flowerLoveDTO.setCreatetime(System.currentTimeMillis());
                flowerLoveDTO.setFlowerId(flowerId);
                Long primaryId = flowerLoveServiceImpl.addLove(flowerLoveDTO);
                flowerLoveDTO.setId(primaryId);
                if(primaryId > 0){
                    reponse = new AjaxResponse<FlowerLoveDTO>(AjaxError.PRAISESUCCESS.getCode(), AjaxError.PRAISESUCCESS.getMessage());
                    reponse.setT(flowerLoveDTO);
                }else{
                    reponse = new AjaxResponse<FlowerLoveDTO>(AjaxError.PRAISESUCCESS.getCode(), AjaxError.PRAISESUCCESS.getMessage());
                }
            }else{
                flowerLoveServiceImpl.channelLove(id);
            }
        }
        return reponse;
    }
    

}
